#include "TBox.h"
#include "TUI.h"
#include "../utils/Utils.h"



TVBox::TVBox(){
    nMaxNum=0;
    itemHeight=0;
    m_nDistance=0;
    renderStartIndex=0;
    renderNum=0;
    m_pRenderListener=NULL;
    m_pRenderfnSelector=NULL;
}

TVBox::~TVBox(){
}

TVBox* TVBox::create(xml_node<> * pItem){
    TVBox*  ret=TVBox::create();
    ret->initWidthConf(pItem);
    return ret;
}

void TVBox::setRenderTarget(CCObject *rec, SEL_MenuHandler selector){
    
    m_pRenderListener=rec;
    m_pRenderfnSelector=selector;
    
}

void TVBox::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent){
    if(m_bIsScroll){
        
        CCPoint touchLocation = pTouch->getLocation();
        CCPoint preLocation  =pTouch->getPreviousLocation();
        CCPoint curPos  = m_pContainer->getPosition();
        CCPoint nextPos ;
        if(m_nDirection==TScrollH){
            
            float nMoveX= touchLocation.x - preLocation.x;
            nextPos = ccp(curPos.x+ nMoveX, curPos.y);
        }else{
            
            float nMoveY = touchLocation.y - preLocation.y;
            nextPos = ccp(curPos.x,(curPos.y + nMoveY));
        }
        
        m_pContainer->setPosition(nextPos);
        checkVisible();
        
        refreshPanel(renderStartTag);
    }
    
}


void TVBox::moveToItem(TPanelItem* item,bool check){
    if(!item){
        return;
    }
    TScrollPanel::moveToPos(item->getPosition(),check);
    refreshPanel(renderStartTag);
}

void TVBox::moveToTop(){
    TScrollPanel::moveToTop();
    refreshPanel(renderStartTag);
}

void TVBox::moveToBottom(){
    TScrollPanel::moveToBottom();
    refreshPanel(renderStartTag);
}
void TVBox::refreshPanel(int startIdx){
    
    renderStartIndex=startIdx;
    if(itemHeight==0){ 
        renderNum=10;
    }else{
        
        renderNum=getContentSize().height/itemHeight+2;
    }
    
    if(m_pRenderListener && m_pRenderfnSelector){ 
        (m_pRenderListener->*m_pRenderfnSelector)(this);
    }
}

void TVBox::checkItemNum(int num){
    if(num>nMaxNum){
        expand(num-nMaxNum);
    }
}

void TVBox::initWidthConf(xml_node<> * pItem){
    TScrollPanel::initWidthConf(pItem); 
    string scrollDir;
    readAttrString(pItem, "itemName", itemName);
   
     
    m_nDistance=readAttrInt(pItem, "dis");
    nMaxNum=readAttrInt(pItem, "maxNum");
    
    for(int i=0;i<nMaxNum;i++){
        TPanelItem* item=TUI::sharedDirector()->createUI(itemName);
        item->setVisible(false);
        addChildItem(item);
        TPanel* panel=dynamic_cast<TPanel*>(item);
        if(panel && panel->confPath.size()!=0){
            panel->loadConf(panel->confPath.c_str());
        }
        item->setTag(i+1);
        itemHeight=item->getContentSize().height;
         
        
    }
    layout();
}

void TVBox::expand(int num){
    Trace("=======expand=======");
    TraceInt(num);
    for(int i=nMaxNum;i<nMaxNum+num;i++){
        TPanelItem* item=TUI::sharedDirector()->createUI(itemName);
        item->setVisible(false);
        addChildItem(item);
        TPanel* panel=dynamic_cast<TPanel*>(item);
        if(panel && panel->confPath.size()!=0){
            panel->loadConf(panel->confPath.c_str());
        }
        item->setTag(i+1);
        
    } 
    nMaxNum+=num;
    layout(); 
}

void TVBox::layout(int num){
    int startY=0;
    for(uint32 i=0;i< nMaxNum;i++){
        TPanelItem* item=(TPanelItem* ) getItemByTag(i+1);
        if(i<num){ 
            item->setVisible(true);
            item->setPositionY(- startY );
            startY+=item->getContentSize().height+ m_nDistance;
        }else{
            item->setPositionY(- 200 );
        }
    }
    
    checkVisible();
    calculateRealHeight(10);
}
void TVBox::layout(){
    int startY=0;
    for(uint32 i=0;i< nMaxNum;i++){
        TPanelItem* item=(TPanelItem* ) getItemByTag(i+1); 
        item->setVisible(true); 
        item->setPositionY(- startY );
        startY+=item->getContentSize().height+ m_nDistance; 
    }
     
    checkVisible();
    calculateRealHeight();
    
}
